#!/bin/sh

if [ `uname` = FreeBSD ]; then
  GETOPT=/usr/local/bin/getopt
else
  GETOPT=getopt
fi

eval set -- "$(${GETOPT} -o i: --long id:,cluster: -- $@)"

while true ; do
	case "$1" in
		-i|--id) id=$2; shift 2 ;;
		--cluster) cluster=$2; shift 2 ;;
		--) shift ; break ;;
	esac
done

if [ -z "$id"  ]; then
    echo "Usage: $0 [OPTIONS]"
    echo "--id/-i ID        set ID portion of my name"
    echo "--cluster NAME    set cluster name (default: ceph)"
    exit 1;
fi

if [ "$Cache"x = yesx ];then
    Cache_Disk=`ls -l $Cache_Disk_Path |awk -F '/' '{print $NF}'`
    Cache_Disk_SCSI_ID=`lsscsi|grep disk|awk '{print $1, $NF}'|sed -n 's/\[//g;s/\]//g;s#/dev/##g;s/\ /-/gp'|grep -w "$Cache_Disk"|awk -F '-' '{print $1}'`
    if [ "$Enable_BCache"x = yesx ];then
        BCache_Device_UUID=`bcache-super-show $Data_Disk_Path|grep -w 'dev.uuid'|awk '{print $NF}'`
        BCache_Device_by_uuid="/dev/bcache/by-uuid/$BCache_Device_UUID"
        BCache_Device=`ls -l /dev/bcache/by-uuid/$BCache_Device_UUID|awk -F '/' '{print $NF}'`
        Block_BCache_Part=`ls -l $Block_BCache_Part_Path |awk -F '/' '{print $NF}'`
    fi
else
    Block_Data_Part=`ls -l $Block_Data_Part_Path |awk -F '/' '{print $NF}'`
fi
Data_Disk=`ls -l $Data_Disk_Path |awk -F '/' '{print $NF}'`
Data_Disk_SCSI_ID=`lsscsi|grep disk|awk '{print $1, $NF}'|sed -n 's/\[//g;s/\]//g;s#/dev/##g;s/\ /-/gp'|grep -w "$Data_Disk"|awk -F '-' '{print $1}'`
Block_WAL_Part=`ls -l $Block_WAL_Part_Path |awk -F '/' '{print $NF}'`
Block_DB_Part=`ls -l $Block_DB_Part_Path |awk -F '/' '{print $NF}'`

#env file to systemd service unit EnvironmentFile
#source "/var/lib/ceph/osd-mapping/osd.$id"
#echo "/var/lib/ceph/osd-mapping/osd.$id"
chown_osd_device(){
    Devices_Len=
    if [ "$Cache"x = yesx ];then
        echo "Cache_Disk:$Cache_Disk"
        echo "Data_Disk:$Data_Disk"
        echo "Block_DB_Part:$Block_DB_Part"
        echo "Block_WAL_Part:$Block_WAL_Part"
        Devices="$Data_Disk $Block_DB_Part $Block_WAL_Part $Cache_Disk"
        if [ "$nable_BCache"x = yesx ];then
            echo "Block_BCache_Part:$Block_BCache_Part"
            echo "BCache_Device:$BCache_Device"
            Devices="$Devices $Block_BCache_Part $BCache_Device"
            for i in $Devices;do
                Devices_Len=$((Devices_Len+1))
            done
            if [ "$Devices_Len"x != 6x ];then
                exit 1
            fi
        else
            for i in $Devices;do
                Devices_Len=$((Devices_Len+1))
            done
            if [ "$Devices_Len"x != 4x ];then
                exit 1
            fi
        fi
    else
        echo "Data_Disk:$Data_Disk"
        echo "Block_DB_Part:$Block_DB_Part"
        echo "Block_WAL_Part:$Block_WAL_Part"
        echo "Block_Data_Part:$Block_Data_Part"
        Devices="$Data_Disk $Block_DB_Part $Block_WAL_Part $Block_Data_Part"
        for i in $Devices;do
            Devices_Len=$((Devices_Len+1))
        done
        if [ "$Devices_Len"x != 4x ];then
            exit 1
        fi
    fi

    for i in $Devices;do
        if echo $i | egrep "hd*|nvme*|sd*|vd*|xvd*|bcache*|cciss*|dasd*|ubd*|ubi*|scm*|pmem*|nbd*|zd*";then
            if [ -b /dev/$i ];then
                chown -R ceph:ceph /dev/$i
                ls -l /dev/$i
            else
                echo $i && exit 1
            fi
        else
            echo $i && exit 1
        fi
    done

    Devices_Path_Len=
    if [ "$Cache"x = yesx ];then
        echo "Cache_Disk_Path:$Cache_Disk_Path"
        echo "Data_Disk_Path:$Data_Disk_Path"
        echo "Block_DB_Part_Path:$Block_DB_Part_Path"
        echo "Block_WAL_Part_Path:$Block_WAL_Part_Path"
        Devices_Path="$Data_Disk_Path $Block_DB_Part_Path $Block_WAL_Part_Path $Cache_Disk_Path"
        if [ "$Enable_BCache"x = yesx ];then
            echo "Block_BCache_Part_Path:$Block_BCache_Part_Path"
            Devices_Path="$Devices_Path $Block_BCache_Part_Path"
            for i in $Devices_Path;do
                Devices_Path_Len=$((Devices_Path_Len+1))
            done
            if [ "$Devices_Path_Len"x != 5x ];then
                exit 1
            fi
        else

            for i in $Devices_Path;do
                Devices_Path_Len=$((Devices_Path_Len+1))
            done
            if [ "$Devices_Path_Len"x != 4x ];then
                exit 1
            fi
        fi
    else
        echo "Data_Disk_Path:$Data_Disk_Path"
        echo "Block_DB_Part_Path:$Block_DB_Part_Path"
        echo "Block_WAL_Part_Path:$Block_WAL_Part_Path"

        Devices_Path="$Data_Disk_Path $Block_DB_Part_Path $Block_WAL_Part_Path $Block_Data_Part_Path"
        for i in $Devices;do
            Devices_Path_Len=$((Devices_Path_Len+1))
        done
        if [ "$Devices_Path_Len"x != 4x ];then
            exit 1
        fi
    fi

    for i in $Devices_Path;do
        if ls -l $i | egrep "hd*|nvme*|sd*|vd*|xvd*|bcache*|cciss*|dasd*|ubd*|ubi*|scm*|pmem*|nbd*|zd*";then
            if [ -L $i ];then
                chown -H ceph:ceph $i
                ls -l $i
            else
                echo $i && exit 1
            fi
        else
            echo $i && exit 1
        fi
    done
}

chown_osd_device

if [ "$Enable_BCache"x = yesx ];then
    echo writeback > /sys/block/$BCache_Device/bcache/cache_mode
    echo 0 > /sys/block/$BCache_Device/bcache/sequential_cutoff
    echo 0 > /sys/block/$BCache_Device/bcache/cache/congested_read_threshold_us
    echo 0 > /sys/block/$BCache_Device/bcache/cache/congested_write_threshold_us
    echo fifo > /sys/block/$BCache_Device/bcache/cache/cache0/cache_replacement_policy
    echo 1 > /sys/block/$BCache_Device/bcache/cache/internal/copy_gc_enabled
    echo "8192" > /sys/block/$BCache_Device/queue/read_ahead_kb
    echo "40" > /sys/block/$BCache_Device/bcache/writeback_percent
else
    echo 'Cache=yes Enable_BCache=no'
fi

if [ "$Cache"x = yesx ];then
    NR_REQUESTS=`cat /sys/block/$Cache_Disk/queue/nr_requests`
    if [ $NR_REQUESTS -lt 1024 ];then
        echo '1024' > /sys/block/$Cache_Disk/queue/nr_requests
    else
        echo "nr_requests $NR_REQUESTS"
    fi
    Cache_Scheduler=`cat /sys/block/$Cache_Disk/queue/scheduler |awk -F']' '{print $1}'|awk -F'[' '{print $NF}'`
    if [ "$Cache_Scheduler"x = nonex ];then
        echo Cache_Scheduler $Cache_Scheduler
    else
        echo 'none' > /sys/block/$Cache_Disk/queue/scheduler
    fi
    echo 'write through' > /sys/block/$Cache_Disk/queue/write_cache
    echo 'write through' > /sys/block/$Cache_Disk/device/scsi_disk/$Cache_Disk_SCSI_ID/cache_type
fi

echo 'write through' > /sys/block/$Data_Disk/queue/write_cache
echo "8192" > /sys/block/$Data_Disk/queue/read_ahead_kb
#echo 'write through' > /sys/block/$Data_Disk/device/scsi_disk/$Data_Disk_SCSI_ID/cache_type

data="/var/lib/ceph/osd/${cluster:-ceph}-$id"

# assert data directory exists - see http://tracker.ceph.com/issues/17091
if [ ! -d "$data" ]; then
    echo "OSD data directory $data does not exist; bailing out." 1>&2
    exit 1
fi

if [ "$Enable_BCache"x = yesx ];then
    ceph-bluestore-tool prime-osd-dir --path $data --no-mon-config --dev /dev/$Block_BCache_Part

    if [ -h "$data/block" ];then 
        echo "the block symbol exists"
    else
        ln -s /dev/$BCache_Device $data/block
    fi
elif [ "$Cache"x = yesx ];then
    ceph-bluestore-tool prime-osd-dir --path $data --no-mon-config --dev /dev/$Data_Disk
    if [ -h "$data/block" ];then
        echo "the block symbol exists"
    else
        ln -s $Block_Data_Part_Path $data/block
    fi
else
    ceph-bluestore-tool prime-osd-dir --path $data --no-mon-config --dev /dev/$Block_Data_Part
    if [ -h "$data/block" ];then
        echo "the block symbol exists"
    else
        ln -s $Block_Data_Part_Path $data/block
    fi
fi

if [ -h "$data/block.db" ];then
    echo "the block symbol exists"
else
    ln -s $Block_DB_Part_Path $data/block.db
fi
if [ -h "$data/block.wal" ];then
    echo "the block symbol exists"
else
    ln -s $Block_WAL_Part_Path $data/block.wal
fi
chown -R ceph:ceph $data


journal="$data/journal"

if [ -L "$journal" -a ! -e "$journal" ]; then
    udevadm settle --timeout=5 || :
    if [ -L "$journal" -a ! -e "$journal" ]; then
        echo "ceph-osd(${cluster:-ceph}-$id): journal not present, not starting yet." 1>&2
        exit 0
    fi
fi

# ensure ownership is correct
owner=`stat -c %U $data/.`
if [ $owner != 'ceph' -a $owner != 'root' ]; then
    echo "ceph-osd data dir $data is not owned by 'ceph' or 'root'"
    echo "you must 'chown -R ceph:ceph ...' or similar to fix ownership"
    exit 1
fi


exit 0

